﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sinacor.Infra.Tools.ServiceRegistration.Business
{
    public class Parameter
    {
        #region Attributes

        private Data.Parameter _resourceAccess;
        private Business.Type _businessType;
        private object locker = new object();

        #endregion

        #region Constructor

        public Parameter()
        {
            this._resourceAccess = new Data.Parameter();
            this._businessType = new Business.Type();
        }

        #endregion

        #region Methods

        public List<Entity.Parameter> SelectParameter(Entity.Parameter entity)
        {
            return _resourceAccess.SelectParameter(entity, true);
        }

        public Entity.Parameter PersistParameter(Entity.Parameter entity)
        {
            Entity.Parameter parameter = new Entity.Parameter() { ParameterName = entity.ParameterName, Method = entity.Method };
            List<Entity.Parameter> parameters = null;

            parameters = _resourceAccess.SelectParameter(parameter, false);

            if (parameters.Count == 0)
            {
                parameter = InsertParameter(entity);
            }
            else
            {
                parameter = parameters[0];

                if (!string.IsNullOrEmpty(entity.Description))
                    parameter.Description = entity.Description;

                parameter.IsCollection = entity.IsCollection;
                parameter.Type = entity.Type;
                parameter.DateChange = entity.DateChange;
                parameter.DataOrigin = entity.DataOrigin;
                UpdateParameter(parameter);
            }

            return parameter;
        }

        public Entity.Parameter InsertParameter(Entity.Parameter entity)
        {
            if (!entity.Type.TypeId.HasValue)
            {
                if (!string.IsNullOrEmpty(entity.Type.FullTypeName))
                    entity.Type = _businessType.PersistType(entity.Type);
                else
                {
                    entity.Type.FullTypeName = "System.Void";
                    entity.Type = _businessType.PersistType(entity.Type);
                }
            }

            entity.ParameterId = this.GetNextParameterId();
            return _resourceAccess.InsertParameter(entity);
        }

        public void UpdateParameter(Entity.Parameter entity)
        {
            if (!entity.Type.TypeId.HasValue)
            {
                if (string.IsNullOrEmpty(entity.Type.FullTypeName))
                    entity.Type.FullTypeName = "System.Void";

                entity.Type = _businessType.PersistType(entity.Type);
            }

            _resourceAccess.UpdateParameter(entity);
        }

        public void DeleteParameter(Entity.Parameter entity)
        {
            _resourceAccess.DeleteParameter(entity);
        }
        
        public int GetNextParameterId()
        {
            int nextId = 0;

            lock (locker)
            {
                nextId = _resourceAccess.GetNextParameterId();
            }

            return nextId;
        }

        #endregion
    }
}
